package MyGO.controller;

import MyGO.Data.Amount;
import MyGO.entity.Doctor;
import MyGO.service.IAmountService;
import MyGO.service.IDoctorService;
import MyGO.service.impl.AmountServiceImpl;
import MyGO.service.impl.DoctorServiceImpl;
import MyGO.utils.enumerastion.Department;
import MyGO.utils.enumerastion.DoctorLevel;
import MyGO.utils.enumerastion.VisTiOnDay;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.Setter;

import java.io.IOException;

import static MyGO.controller.MyController.PushWindow.pushOnlyPrompt;

public class RegistDoctorViewController {
    @Setter
    private Stage MyStage;

    @Setter
    private LoginControl loginControl;


    @FXML
    private ChoiceBox<Department> co_dept;
    @FXML
    private ChoiceBox<DoctorLevel> co_level;
    @FXML
    private ChoiceBox<VisTiOnDay> co_time;
    @FXML
    private Button regist;
    @FXML
    private TextField tf_amount;
    @FXML
    private TextField tf_gender;
    @FXML
    private TextField tf_name;
    @FXML
    private TextField tf_password;

    private IDoctorService doctorService = new DoctorServiceImpl();
    private IAmountService amountService = new AmountServiceImpl();

    @FXML
    public void initialize() {
        co_dept.getItems().addAll(Department.getDeptList());
        co_level.getItems().addAll(DoctorLevel.getDocLevList());
        co_time.getItems().addAll(VisTiOnDay.getVisTiOnDayList());
    }


    @FXML
    void RegistAct(ActionEvent event) throws IOException {
        if (tf_name.getText() == null || tf_gender.getText() == null || tf_password.getText() == null ||
            tf_amount.getText() == null || co_dept.getValue() == null || co_level.getValue() == null  ||
            co_time.getValue() == null) {
            pushOnlyPrompt("/MyGO/fxml/prompt_view_check_medicine.fxml", "请填入完整的信息", 200.0, 100.0);
        } else {
            int ID = Integer.parseInt(doctorService.selevtAllDoctor().get(doctorService.selevtAllDoctor().size() - 1).getId());
            Doctor newDoctor = new Doctor(String.valueOf(ID), 2, 0, tf_name.getText(), tf_password.getText(),
                    co_dept.getValue(), co_level.getValue(), co_time.getValue(), co_level.getValue().getFee());
            Amount newamount = new Amount(tf_amount.getText(), tf_password.getText(), String.valueOf(ID));

            doctorService.addDoctor(newDoctor);
            amountService.addAmount(newamount);

            pushOnlyPrompt("/MyGO/fxml/prompt_view_check_medicine.fxml", "注册成功", 200.0, 100.0);

            tf_amount.clear();
            tf_password.clear();
            tf_gender.clear();
            tf_name.clear();
            co_dept.getItems().clear();
            co_time.getItems().clear();
            co_level.getItems().clear();
            initialize();
        }

    }

}
